#!/usr/bin/python
# -*- coding: UTF-8 -*-
'''
Created on 2016-11-8
 
@author: reggie.dai

'''

import os,platform,subprocess,csv,time

def get():
    todayDate=time.strftime('%Y%m%d',time.localtime(time.time()))
    csvfile=open(os.path.join(os.getcwd())+'\\DisplayedTime-'+todayDate+'.csv','wb')
    #spamwriter = csv.writer(csvfile, delimiter=' ',quotechar='|', quoting=csv.QUOTE_MINIMAL)
    spamwriter = csv.writer(csvfile, dialect='excel')
    spamwriter.writerow(['ActivityName', 'DisplayedTime'])

    popen = subprocess.Popen('adb logcat -c', stdout = subprocess.PIPE)
    popen = subprocess.Popen('adb logcat', stdout = subprocess.PIPE)
    returncode = popen.poll()
    while returncode is None:
        content=popen.stdout.readline()
        if 'Displayed' in content:
            #print(content)
            s=content.split('Displayed')[len(content.split('Displayed'))-1].strip()
            activityName=s.split(":")[0]
            print(activityName)
            displayedTime=s.split('+')[1].strip().split('ms')[0]
            print(displayedTime)
            spamwriter.writerow([activityName,displayedTime])
  
if __name__ == '__main__':
   get()
